home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / pdfclock.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  3KB  |  157 lines

  1. /* pdfclock.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  *
  4.  * A little PDFlib application to draw an analog clock.
  5.  */
  6.  
  7. #ifdef DOS
  8. #include <process.h>
  9. #endif
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <time.h>
  15.  
  16. #ifdef POSIX
  17. #include <unistd.h>
  18. #endif
  19.  
  20. #include "pdf.h"
  21.  
  22. static void
  23. usage(void)
  24. {
  25.     fprintf(stderr, 
  26.     "pdfclock - generate clock in PDF format. (C) Thomas Merz 1997-98\n");
  27.     fprintf(stderr, "usage: pdfclock [-c pagecount] -o outfile\n");
  28.  
  29.     exit(1);
  30. }
  31.  
  32. #define RADIUS        200.0
  33. #define MARGIN        20.0
  34.  
  35. void
  36. main(int argc, char *argv[])
  37. {
  38.     PDF_info    *info;
  39.     FILE    *pdffile = NULL;
  40.     PDF        *p;
  41.     int        opt;
  42.     float    alpha;
  43.     time_t    timer;
  44.     struct tm    ltime;
  45.     int        pagecount = 1;
  46.     
  47.     info        = PDF_get_info();
  48.     info->Title        = "PDF Clock";
  49.     info->Creator       = "pdfclock";
  50.  
  51.     while ((opt = getopt(argc, argv, "c:o:")) != -1)
  52.     switch (opt) {
  53.         case 'c':
  54.         pagecount = atoi(optarg);
  55.         if (pagecount < 0)
  56.             pagecount = 1;
  57.         break;
  58.  
  59.         case 'o':
  60.         pdffile = fopen(optarg, WRITEMODE);
  61.         if (pdffile == NULL) {
  62.             fprintf(stderr, 
  63.             "pdfclock error: cannot open output file %s.\n",optarg);
  64.             usage();
  65.         }
  66.         break;
  67.     }
  68.  
  69.     if (pdffile == (FILE *) NULL)
  70.     usage();
  71.  
  72.     p = PDF_open(pdffile, info);
  73.  
  74.     while (pagecount-- > 0) {
  75.     PDF_begin_page(p, 2 * (RADIUS + MARGIN), 2 * (RADIUS + MARGIN));
  76.     
  77.     PDF_set_transition(p, trans_wipe);
  78.     PDF_set_duration(p, 0.5);
  79.  
  80.     PDF_translate(p, RADIUS + MARGIN, RADIUS + MARGIN);
  81.     PDF_setrgbcolor(p, 0.0, 0.0, 1.0);
  82.     PDF_save(p);
  83.  
  84.     /* minute strokes */
  85.     PDF_setlinewidth(p, 2.0);
  86.     for (alpha = 0; alpha < 360; alpha += 6)
  87.     {
  88.         PDF_rotate(p, 6.0);
  89.         PDF_moveto(p, RADIUS, 0.0);
  90.         PDF_lineto(p, RADIUS-MARGIN/3, 0.0);
  91.         PDF_stroke(p);
  92.     }
  93.  
  94.     PDF_restore(p);
  95.     PDF_save(p);
  96.  
  97.     /* 5 minute strokes */
  98.     PDF_setlinewidth(p, 3.0);
  99.     for (alpha = 0; alpha < 360; alpha += 30)
  100.     {
  101.         PDF_rotate(p, 30.0);
  102.         PDF_moveto(p, RADIUS, 0.0);
  103.         PDF_lineto(p, RADIUS-MARGIN, 0.0);
  104.         PDF_stroke(p);
  105.     }
  106.  
  107.     time(&timer);
  108.     ltime = *localtime(&timer);
  109.  
  110.     /* draw hour hand */
  111.     PDF_save(p);
  112.     PDF_rotate(p, 
  113.         (float)(-((ltime.tm_min/60.0) + ltime.tm_hour - 3.0) * 30.0));
  114.     PDF_moveto(p, -RADIUS/10, -RADIUS/20);
  115.     PDF_lineto(p, RADIUS/2, 0.0);
  116.     PDF_lineto(p, -RADIUS/10, RADIUS/20);
  117.     PDF_closepath(p);
  118.     PDF_fill(p);
  119.     PDF_restore(p);
  120.  
  121.     /* draw minute hand */
  122.     PDF_save(p);
  123.     PDF_rotate(p,
  124.         (float) (-((ltime.tm_sec/60.0) + ltime.tm_min - 15.0) * 6.0));
  125.     PDF_moveto(p, -RADIUS/10, -RADIUS/20);
  126.     PDF_lineto(p, RADIUS * 0.8, 0.0);
  127.     PDF_lineto(p, -RADIUS/10, RADIUS/20);
  128.     PDF_closepath(p);
  129.     PDF_fill(p);
  130.     PDF_restore(p);
  131.  
  132.     /* draw second hand */
  133.     PDF_setrgbcolor(p, 1.0, 0.0, 0.0);
  134.     PDF_setlinewidth(p, 2);
  135.     PDF_save(p);
  136.     PDF_rotate(p, (float) -((ltime.tm_sec - 15.0) * 6.0));
  137.     PDF_moveto(p, -RADIUS/5, 0.0);
  138.     PDF_lineto(p, RADIUS, 0.0);
  139.     PDF_stroke(p);
  140.     PDF_restore(p);
  141.  
  142.     /* draw little circle at center */
  143.     PDF_circle(p, 0, 0, RADIUS/30);
  144.     PDF_fill(p);
  145.  
  146.     PDF_restore(p);
  147.  
  148.     PDF_end_page(p);
  149.  
  150.     sleep(1);        /* we want to see the clock hand move */
  151.     }
  152.  
  153.     PDF_close(p);
  154.  
  155.     exit(0);
  156. }
  157.